using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gko.Utils
{
	public static class ObjectExtensions
	{
		public static bool In<T>(this T value, params T[] values) where T : IEquatable<T>
		{
			if(values == null) {
				return false;
			}
			foreach (var v in values) {
				if(value.IsNull() && v.IsNull()) {
					return true;
				}
				if(v.IsNull()) {
					continue;
				}
				if(v.Equals(value)) {
					return true;
				}
			}
			return false;
		}

		public static bool IsNull<T>(this T value)
		{
			return ReferenceEquals(value, null);
		}

		public static bool IsNull(this IntPtr value)
		{
			return value == IntPtr.Zero;
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flag"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flags"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flag")]
		public static bool HasFlag(this int flags, int flag)
		{
			return (flags & flag) == flag;
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flag"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flags"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flag")]
		public static int SetFlag(this int flags, int flag)
		{
			return flags | flag;
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flag"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flags"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flag")]
		public static int ClearFlag(this int flags, int flag)
		{
			return flags & ~flag;
		}
	}
}